package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.common.domain.EduComment;
import com.jf3q.common.domain.EduCourse;
import com.jf3q.common.domain.EduMember;
import com.jf3q.common.utils.JwtTokenUtil;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.front.service.EduCommentService;
import com.jf3q.front.service.impl.CourseServiceImpl;
import com.jf3q.front.service.impl.MemberServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "评论")
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    EduCommentService eduCommentService;
    @Autowired
    MemberServiceImpl memberService;
    @Autowired
    CourseServiceImpl courseService;

    @PostMapping("/getListComment/{cid}")
    @Operation(summary = "查看评论")
    public ResultVo getListOrder(@PathVariable String cid,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = Integer.MAX_VALUE+"") Integer pageSize){

        Page<EduComment> page=eduCommentService.page(new Page<>(pageNum,pageSize),new LambdaQueryWrapper<EduComment>().eq(EduComment::getMemberId,cid));
        return ResultVo.success("",page);
    }

    @PostMapping("/delComment/{commentId}")
    @Operation(summary = "删除评论")
    public ResultVo delOrder(@PathVariable String commentId){
        try {
            eduCommentService.removeById(commentId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultVo.success("删除成功","");
    }

    @GetMapping("/xiang/{cid}")
    @Operation(summary = "评论详情")
    public ResultVo getOrder(@PathVariable String cid) {
        EduComment eduComment= eduCommentService.getById(cid);
        return ResultVo.success("订单详情", eduComment);
    }

    @PostMapping("/saveOrUpdateComment")
    @Operation(summary = "修改评论")
    public ResultVo saveOrUpdateComment(@RequestBody EduComment eduComment){
        try {
            eduCommentService.saveOrUpdate(eduComment);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultVo.success("添加成功",null);
    }

    @GetMapping("/getComments/{id}")
    public ResultVo getComments(@PathVariable String id){
        List<EduComment> list = eduCommentService.list(new LambdaQueryWrapper<EduComment>().eq(EduComment::getCourseId, id));
        return ResultVo.success(list);
    }

    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @PostMapping("/create")
    @Operation(summary = "发表")
    public ResultVo create(@RequestBody EduComment comment, HttpServletRequest request){
        String mobile = jwtTokenUtil.getUserNameFromToken(request.getHeader("token"));
        EduMember member = memberService.getOne(new LambdaQueryWrapper<EduMember>().eq(EduMember::getMobile, mobile));
        EduCourse course = courseService.getById(comment.getCourseId());
        comment.setTeacherId(course.getTeacherId());
        comment.setMemberId(member.getId());
        comment.setNickname(member.getNickname());
        comment.setAvatar(member.getAvatar());
        return eduCommentService.save(comment)?ResultVo.success():ResultVo.error();
    }

}
